/**
 * Copyright (c) 2007,中企动力华南研发中心
 * All rights reserved.
 */
package com.ce.pms.common.util;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;

/**
 * 功能描述：JSON工具类库
 * @author 罗军林 创建日期：2008-7-14
 */
public class BeanJsonUtil {

	/**
	 * 功能描述:根据给定的OBJECT，将生成对应的json字符串，先试用，然后再抽取为公用
	 * @param obj
	 * @return
	 * @throws IntrospectionException
	 * @throws IllegalAccessException
	 * @throws InvocationTargetException
	 * Create author:罗军林<br>
	 * Create on:2008-7-8<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public static  String BeanToJsonStr(Object obj)throws IntrospectionException, IllegalAccessException,InvocationTargetException
	{
		StringBuffer json = new StringBuffer("{");
		BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
		PropertyDescriptor p[] = beanInfo.getPropertyDescriptors();
		for (PropertyDescriptor item : p) {
			if (!item.getName().equals("class")) {
				json.append("\"" + item.getName() + "\" : ");
				Object result = item.getReadMethod().invoke(obj,
						new Object[] {});
//				if (result != null) {
					json.append("\"" + (result == null ? "" : result) + "\",");
//				} else if (result == null) {
//					json.append("" + ",");
//				}
			}
		}
		json.deleteCharAt(json.length() - 1);
		json.append("}");
		return json.toString();
	}

}
